package com.xinzhidi.web.dataManagement.service;

import com.xinzhidi.common.core.domain.AjaxResult;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.web.build.service.IBuildUnitService;
import com.xinzhidi.web.dataManagement.domain.bo.FiledControl;
import com.xinzhidi.web.dataManagement.domain.po.DataManagementPo;
import com.xinzhidi.web.dataManagement.domain.vo.DataManagementVo;
import com.xinzhidi.web.dataManagement.mapper.DataManagementMapper;
import com.xinzhidi.web.netty.utils.SecurityAdminUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;


/**
 * 数据管理Service业务层处理
 *
 * @author cx
 * @date 2023-06-09
 */
@Service
@RequiredArgsConstructor
public class DataManagementService {

    private final DataManagementMapper managementMapper;

    private final IBuildUnitService buildUnitService;

    /**
     * 楼栋-详细数据查询
     *
     * @param po 查询条件
     * @return 列表
     */
    public TableDataInfo<DataManagementVo> listBuild(DataManagementPo po) {
        List<DataManagementVo> list = Lists.newArrayList();
        if(po.getSelectType() == 1){
            //查询热量表数据
            list = managementMapper.listHeatBuild(po);
        }else if(po.getSelectType() == 2){
            //查阀门数据
            list = managementMapper.listValveBuild(po);
        }
        else if(po.getSelectType() == 3){
            //查室温数据
            list = managementMapper.listRoomTemperatureBuild(po);
        }

        return TableDataInfo.getDataTable(list);
    }

    /**
     * 单元-详细数据查询
     *
     * @param po 查询条件
     * @return 列表
     */
    public TableDataInfo<DataManagementVo> listBuildUnit(DataManagementPo po) {
        List<DataManagementVo> list = Lists.newArrayList();
        if(po.getSelectType() == 1){
            //查询单元热量表数据
            list = managementMapper.listHeatUnit(po);
        }else if(po.getSelectType() == 2){
            //查阀门数据
            list = managementMapper.listValveUnit(po);
        } else if(po.getSelectType() == 3){
            //查室温数据
            list = managementMapper.listRoomTemperatureUnit(po);
        }
        return TableDataInfo.getDataTable(list);
    }

    /**
     * 住户-详细数据查询
     *
     * @param po 查询条件
     * @return 列表
     */
    public TableDataInfo<DataManagementVo> listHousehold(DataManagementPo po) {
        List<Long> userPlotIds = SecurityAdminUtils.getUserPlotIds();
        po.setUserPlotIds(userPlotIds);
        List<DataManagementVo> list = Lists.newArrayList();
        if(po.getSelectType() == 1){
            //查询热量表数据
            list = managementMapper.listHeatHousehold(po);
        }else if(po.getSelectType() == 2){
            //查阀门数据
            list = managementMapper.listValveHousehold(po);
        }else if(po.getSelectType() == 3){
            list = managementMapper.listTemperatureHousehold(po);
        }

        return TableDataInfo.getDataTable(list);
    }

    public AjaxResult fieldControl(List<FiledControl> list) {
        if (CollectionUtils.isEmpty(list)){
            return new AjaxResult(-1, "操作失败，请选择要展示的数据！");
        }
        FiledControl filedControl = list.get(0);
        managementMapper.delete(filedControl);
        managementMapper.insert(list);
        return new AjaxResult(0, "操作成功");
    }

    public TableDataInfo<FiledControl> fieldControlList(FiledControl list) {
        // 自定义方法
        List<FiledControl> result = managementMapper.fieldControlList(list);
        return TableDataInfo.getDataTable(result);
    }
}
